#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Pandora;
using UnityEditor;
using UnityEngine;

namespace PandoraEditor
{
    public static class TextureUtility
    {
        #region Importer
        public static void ImportUISprite(string path)
        {
            TextureImporterType textureType = TextureImporterType.Sprite;
            SpriteImportMode spriteMode = SpriteImportMode.Single;
            TextureImporterAlphaSource alphaSource = TextureImporterAlphaSource.FromInput;
            TextureWrapMode wrapMode = TextureWrapMode.Clamp;
            TextureImporterCompression compression = TextureImporterCompression.Uncompressed;
            var importerList = PdrFindBase.GetAssetTextureImporters(path).Select(asset => asset.textureImporter);
            var textureImporters = importerList as TextureImporter[] ?? importerList.ToArray();
            string title = "Import Sprite";
            EditorUtility.DisplayProgressBar(title, "Please Wait......", 0);
            for (int i = 0; i < textureImporters.Length; i++)
            {
                string hint = string.Format($"{i + 1}/{textureImporters.Length}");
                EditorUtility.DisplayProgressBar(title, hint, (i + 1) * 1.0f / textureImporters.Length);
                SetTextureImporter(textureImporters[i], textureType, spriteMode, alphaSource, wrapMode, compression);
            }
            EditorUtility.ClearProgressBar();
        }

        public static void ImportAtlasColor(string path, List<SpriteMetaData> spriteMetaDataList = null, bool isCompress = true, SpriteImportMode spriteMode = SpriteImportMode.Multiple)
        {
            TextureImporterType textureType = TextureImporterType.Sprite;
            TextureImporterAlphaSource alphaSource = isCompress ? TextureImporterAlphaSource.None : TextureImporterAlphaSource.FromInput;
            TextureWrapMode wrapMode = TextureWrapMode.Clamp;
            TextureImporterCompression compression = TextureImporterCompression.Compressed;
            foreach (var texImporter in PdrFindBase.GetAssetTextureImporters(path).Select(asset => asset.textureImporter))
            {
                SetTextureImporter(texImporter, textureType, spriteMode, alphaSource, wrapMode, compression);
                AtlasImport.SetPlatformSetting(texImporter);
                if (spriteMetaDataList != null)
                {
                    texImporter.spritesheet = spriteMetaDataList.ToArray();
                    EditorUtility.SetDirty(texImporter);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                    texImporter.SaveAndReimport();
                }
            }
        }

        private static void SetTextureImporter(TextureImporter texImporter, TextureImporterType textureType,
            SpriteImportMode spriteMode, TextureImporterAlphaSource alphaSource, TextureWrapMode wrapMode,
            TextureImporterCompression compression, bool alphaIsTransparency = false, int maxTextureSize = 2048,
            bool mipmap = false, bool readWrite = false, bool sRGBTexture = true, FilterMode filterMode = FilterMode.Bilinear)
        {
            if (texImporter.textureType != textureType || texImporter.spriteImportMode != spriteMode ||
                texImporter.alphaSource != alphaSource || texImporter.wrapMode != wrapMode ||
                texImporter.filterMode != filterMode || texImporter.textureCompression != compression ||
                texImporter.mipmapEnabled != mipmap || texImporter.sRGBTexture != sRGBTexture ||
                texImporter.isReadable != readWrite || texImporter.alphaIsTransparency != alphaIsTransparency ||
                texImporter.maxTextureSize != maxTextureSize)
            {
                texImporter.textureType = textureType;
                texImporter.textureType = textureType;
                texImporter.spriteImportMode = spriteMode;
                texImporter.alphaSource = alphaSource;
                texImporter.wrapMode = wrapMode;
                texImporter.filterMode = filterMode;
                texImporter.textureCompression = compression;
                texImporter.mipmapEnabled = mipmap;
                texImporter.sRGBTexture = sRGBTexture;
                texImporter.isReadable = readWrite;
                texImporter.alphaIsTransparency = alphaIsTransparency;
                texImporter.maxTextureSize = maxTextureSize;
                texImporter.SaveAndReimport();
            }
        }
        #endregion

        #region TextureUtil
        public static void CopyColorTexture(string source, string outColor)
        {
            PdrFileUtil.CopyFile(source, outColor);
            AssetDatabase.Refresh();
        }
        #endregion
    }
}